//
//  ZJZXMailViewController.m
//  Rong360
//
//  Created by tianliwei on 1/3/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "ZJZXMailViewController.h"
#import "ZJZXContentViewController.h"
#import "CityListViewController.h"
#import "APService.h"
@interface ZJZXMailViewController ()

@end

@implementation ZJZXMailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) citySelectionUpdate:(NSString*)selectedCity selectedCityID:(NSString *)selectedCityID{
//    [[NSUserDefaults standardUserDefaults]setObject:selectedCity forKey:kCurrentCity];
//    [[NSUserDefaults standardUserDefaults]setObject:selectedCityID forKey:kCurrentCityID];
    NSString *productPara = [NSString stringWithFormat:@"city_id=%@",selectedCityID];
    NSString *productUrl = [NSString stringWithFormat:@"%@?%@&token=%@",ProductUrl,productPara,[SignUtil stringFromURL:productPara]];
    NSLog(@"productUrl:======> %@\n",productUrl);
    [RequestUtil requestWithType:Product Url:productUrl];
    UIButton *button = (UIButton *)self.navigationItem.rightBarButtonItem.customView;
    [button setTitle:selectedCity forState:UIControlStateNormal];
    [[NSNotificationCenter defaultCenter]postNotificationName:kUpdateCityNotification object:nil userInfo:nil];

}

// 正则判断邮箱
- (BOOL)isMailbox:(NSString *)mail
{
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:mail];
}

- (void)viewWillAppear:(BOOL)animated{
    NSString *currentCity = [[NSUserDefaults standardUserDefaults]objectForKey:kCurrentCity];
    UIButton *cityButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cityButton setBackgroundColor:[UIColor clearColor]];
    [cityButton setTitle:currentCity forState:UIControlStateNormal];
    [cityButton addTarget:self action:@selector(presentCityViewController) forControlEvents:UIControlEventTouchUpInside];
    cityButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    cityButton.frame = CGRectMake(0.0f, 0.0f, 80.0f, 25.0f);
    UIBarButtonItem *cityButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cityButton];
    self.navigationItem.rightBarButtonItem = cityButtonItem;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"专家咨询";
    
    [self.textFieldBackground setImage:[[UIImage imageNamed:@"tf_mailbox"]stretchableImageWithLeftCapWidth:20 topCapHeight:20]];
    
}

- (void)presentCityViewController{
    CityListViewController *cityVC = [[CityListViewController alloc]init];
    cityVC.delegate = self;
    cityVC.dismissStyle = Mail;
    UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:cityVC];
    [self presentModalViewController:naviVC animated:YES];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonClicked:(id)sender {
    [self.mailTextField resignFirstResponder];
    UIButton *btn = (UIButton *)sender;
    switch (btn.tag) {
        case 0:{
            if ([self isMailbox:self.mailTextField.text]) {
                [[NSUserDefaults standardUserDefaults]setObject:self.mailTextField.text forKey:kUserMailBox];
                self.hidesBottomBarWhenPushed = YES;
                [self.mailTextField resignFirstResponder];
                ZJZXContentViewController *zjzxContentVC = [[ZJZXContentViewController alloc]init];
                [self.navigationController pushViewController:zjzxContentVC animated:YES];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"对不起，您输入的邮箱格式有误，请重新输入" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
            
            break;
        }
        case 1:{
            self.hidesBottomBarWhenPushed = YES;
            //[self.mailTextField resignFirstResponder];
            ZJZXContentViewController *zjzxContentVC = [[ZJZXContentViewController alloc]init];
            [self.navigationController pushViewController:zjzxContentVC animated:YES];
            break;
        }
        default:
            break;
    }
}

//- (void)tagsAliasCallback:(int)iResCode tags:(NSSet*)tags alias:(NSString*)alias {
//    NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias);
//}

@end
